import { regBaseMD } from "../MD/GMDBase/RegBaseMD";
import { MD } from "../MD/GMDBase/MD";
import DailyRefresh from "../MD/GMDBase/DailyRefresh";
import { regWeakNetGameBaseMD } from "../MD/GMDWeakNetGameBase/RegWeakNetGameBaseMD";
import defaultStorage from "../MD/GMDBase/defaultStorage"; 
import LCData from "../MD/GMDWeakNetGameBase/LCData";
import { cfg } from "./appCfgDetails";
import { Native } from "../MD/GMDApp/GMDAppIndex";
import { UpdateMgr } from "../MD/GMDApp/GMDAppIndex";
import { regAppMD } from "../MD/GMDApp/GMDAppIndex";
import { txt, txtLocale } from "../MD/GMDBase/SDataID2";
import RewardedVideoMgr from "../MD/GMDAd/RewardedVideoMgr";
import { AnalyticsMgr } from "../MD/GMDAnalytics/AnalyticsMgr";
import AnalyticsPlugin_Umeng from "../MD/GMDAnalytics/Platforms/Umeng/AnalyticsPlugin_Umeng";
import { regAnalyticsMD } from "../MD/GMDAnalytics/RegAnalyticsMD";
import { gcfg } from "../gcfg";
import DbgRewardedVideo from "../MD/GMDDebug/DbgRewardedVideo";
import LocalizedSprite from "../MD/GMDCocosBase/CM/LocalizedSprite"; 


let UpgradeCheckDoing: boolean = false

//执行升级检查
function doUpCheck() {
    if (UpgradeCheckDoing) {
        console.log("更新 跳过检查")
        return
    }
    //console.log("更新 检查更新") 
    UpgradeCheckDoing = true

    UpdateMgr.Check((res) => { })

     //英文版本
     if(true)
     { 
         LocalizedSprite.SpriteLocale(1)
         txtLocale("_EN_US")
     }  
    //console.log("UpdateMgr.LastRes#1",UpdateMgr.LastRes)
    if (UpdateMgr.LastRes && UpdateMgr.LastRes.hasUpdate)//需要更新
    {



        console.log("更新 显示更新提示")

        var LastRes = UpdateMgr.LastRes

        UpdateMgr.NotifyUpWndShow()

        if (LastRes.force)//强制更新
        {
            MD.WndMsgBox.DontClose = true
            MD.WndMsgBox.ShowOK(
                txt(104), //更新提示 
                txt(116),//强制更新
                txt(106),//立即更新
                (isOk) => {
                    UpgradeCheckDoing = false

                    console.log("更新 ShowOK reCall")
                    console.log("更新 UpdateMgr.LastRes", LastRes)

                    //继续显示更新
                    //MD.WndMsgBox.Show()

                    var url = LastRes.url
                    Native.OpenURL(url)

                    //通知界面被关闭
                    //UpdateMgr.NotifyUpWndHide()

                    //一段时间内不再提示更新
                    //UpdateMgr.ClearLastRes()


                }
            )
        } else//非强制更新
        {
            //提示升级
            MD.WndMsgBox.ShowOKCancel(
                txt(104), //更新提示 
                txt(105),//发现新版本，<br/>可前往商店更新
                txt(106),//立即更新
                txt(107),//下次再说
                (isOk) => {
                    UpgradeCheckDoing = false

                    console.log("更新 ShowOKCancel reCall")
                    if (isOk)//选择了更新
                    {
                        console.log("更新 UpdateMgr.LastRes", LastRes)
                        var url = LastRes.url
                        Native.OpenURL(url)
                    }

                    //通知界面被关闭
                    UpdateMgr.NotifyUpWndHide()

                    //一段时间内不再提示更新
                    UpdateMgr.ClearLastRes()
                }
            )//end ShowOkCancel
        }
    } else {
        UpgradeCheckDoing = false
        console.log("更新 无需更新")
    }
}

export default class platformMain {

    public static Start(recall:Function)
    { 
        //英文版本
        if(gcfg.LocaleEn)
        { 
            LocalizedSprite.SpriteLocale(1)
            txtLocale("_EN_US")
        }


        if(gcfg.LocaleEn)//海外地区 Native.GetRegion()=="en"
        {
            gcfg.appVesionCheckUrl = gcfg.appVesionCheckUrl_en
            //gcfg.UtcTimeUrl  = gcfg.UtcTimeUrl_en 

            gcfg.UtcTimeUrlGroup = {
                Priority:[gcfg.UtcTimeUrl_en],//优选地址列表  
                Other:[gcfg.UtcTimeUrl]//其它可选地址列表
            }
        }else//大陆地区
        {
            gcfg.UtcTimeUrlGroup = {
                Priority:[gcfg.UtcTimeUrl],//优选地址列表  
                Other:[gcfg.UtcTimeUrl_en]//其它可选地址列表
            }
        }

      recall()
    }  
      

    //初始化模块,在本地数据可用时回调
    public static InitMD(recall: Function) {

        console.log("App platformMain InitMD")
        regBaseMD()
        regWeakNetGameBaseMD()
        regAppMD()
        regAnalyticsMD()
        

        //设置使用的存储引擎
        MD.Storage = defaultStorage



        //var gLG = new LG("GConn",new WebSktFactory_Cocos())

        //统计系统开始工作
        AnalyticsMgr.Go([new AnalyticsPlugin_Umeng()])

        //设置账号登录系统
        // MD.Account = xiaoziwxAccount

        //应用升级提示
        // AppUpgrage.BindAppUpdateEvt()

        //初始化本地数据，并绑定参数变更事件
        MD.LCData.AutoInitParams()

        
        
        if (!LCData.CanUseLocalData) //本地方案不可用
        {
            //设定本地档案可用标记
            MD.Storage.Set(MD.LCData.ParamName.LC_DATA, "1")

            //在这里初始化用户档案
            //...
            console.log("初始化用户档案")

            //用户注册成功打点
            AnalyticsMgr.SendRegister({account:""})
        }

         

        if (gcfg.Debug && gcfg.UseDbgRewardedVideo)//是否使用调试激励视频插件
            MD.RewardedVideoPlugin = new DbgRewardedVideo()


        //视频奖励管理器开始工作
        RewardedVideoMgr.Go()

        //每日刷新系统开始工作
        DailyRefresh.Go()

        //本地数据绑定服务器同步
        LCData.BindSvrSync()

        //用户登录成功打点
        AnalyticsMgr.SendLoginSuccess({account:""})

        //通知平台初始化完成
        recall()
    }


    //初始化本地数据
    public static initLocalData() { }

    
    //当ui准备就绪时被调用，接口处理完后应回调 recall
    public static OnUIReady(recall: Function) {
        recall()
    }

    //升级检查
    public static StartUpgradeCheck() {
        console.log("更新 StartUpgradeCheck#1")
        doUpCheck()
    }


    public static get cfg(): any {
        return cfg
    }

    public static get tag(): string {
        return 'app'
    }

    public static get IsQWDev(): boolean {
        return false
    }

    static g_SplashAdDone: boolean
}
